package com.ohuang.jiaxiaotong.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.example.myhttp.CallBackString;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.base.BaseAc;

import java.util.HashMap;
import java.util.Objects;

import okhttp3.OkHttpClient;

//更改密码界面
public class ChangePawActivity extends BaseAc {
    private EditText editText_oldpsw, editText_psw, editText_psw2;
    private String name;
    private String oldpsw, password1, password2;
    private Dialog dialogLoading;
    private static final int RE_SUCCESS = 0, RE_FAIL = 1, RE_ERROR = 2, RE_OTHER = 3;
    final OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        name = intent.getStringExtra("username");
        setTitle("更改密码");
        Ohuang.changeStatusBarTextImgColor(this, true);
        Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.mipmap.icon_goback);//设置ActionBar返回按钮

        setContentView(R.layout.activity_change_paw);
        editText_oldpsw = findViewById(R.id.edit_oldPaw_ChangePaw);
        editText_psw = findViewById(R.id.edit_password_ChangePaw);
        editText_psw2 = findViewById(R.id.edit_password2_ChangePaw);
    }


    //发起更改密码请求
    private void PostHttpRequest() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username", name);
        map.put("password", oldpsw);
        map.put("password3", password1);
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/change", map, new CallBackString() {
            @Override
            public void success(String ojb) {
                String code = ojb;
                if ("success".equals(code)) {
                    myHandler.sendEmptyMessage(RE_SUCCESS);
                } else if ("error".equals(code)) {
                    myHandler.sendEmptyMessage(RE_FAIL);
                } else {
                    myHandler.sendEmptyMessage(RE_OTHER);
                }
            }



            @Override
            public void fail(String s) {
                myHandler.sendEmptyMessage(RE_ERROR);
            }
        });


    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case RE_SUCCESS:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改成功", "密码更改成功", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                    break;
                case RE_FAIL:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改失败", "密码更改失败,请检查旧密码是否正确");
                    break;
                case RE_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改失败", "网络连接错误");
                    break;
                case RE_OTHER:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(ChangePawActivity.this, "更改失败", "返回值错误");
                    break;

            }
        }
    };

    //更改密码按钮
    public void ChangePawClick(View view) {
        oldpsw = editText_oldpsw.getText().toString();
        password1 = editText_psw.getText().toString();
        password2 = editText_psw2.getText().toString();
        if (oldpsw.isEmpty() || password1.isEmpty() || password2.isEmpty()) {
            Ohuang.dialogshow(this, "更改失败", "密码不能为空。");
            return;
        }
        if (!password1.equals(password2)) {
            Ohuang.dialogshow(this, "更改失败", "两次密码输入不一致。");
            return;
        }
        dialogLoading = Ohuang.dialogLoadingshow(ChangePawActivity.this, "更改中");

        PostHttpRequest();//发起更改密码请求


    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {//左上角返回按钮
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
